home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 289_01.zip / GETMOV.C < prev    next >
Text File  |  1993-04-26  |  5KB  |  191 lines

  1. /*-----------------------------------------------------------------------------
  2. GETMOV.C
  3.  
  4. This file contains the functions that read the player's move, verifies the
  5. move and displays the new screen.
  6.  
  7. Revision History
  8. ----------------
  9. Jon Ward    10 Dec 1988    Initial Revision
  10. Gary Culp     2 Jan 1989    changed return type & value of check_for_input
  11.                   added code to update curnt_bd
  12.                   changed method of validating move
  13. Jon Ward     7 Jan 1989    Corrected a bug -- forgot to initialize 
  14.                   curnt_move.
  15. -----------------------------------------------------------------------------*/
  16.  
  17. #include <conio.h>
  18. #include <ctype.h>
  19.  
  20. #include "othello.h"
  21.  
  22.  
  23. /*-----------------------------------------------------------------------------
  24.                         Local Variable Declarations
  25. -----------------------------------------------------------------------------*/
  26.  
  27. STATIC int move_row;        /* entered row portion of move */
  28. STATIC int move_col;        /* entered col portion of move */
  29. STATIC int input_flag;        /* first pass for move logic flag */
  30.  
  31.  
  32. /*-----------------------------------------------------------------------------
  33. move_type check_for_input (void);
  34.  
  35. This function checks for and processes player input.  When the player presses
  36. a key, it is read by this routine.  If the keystroke represents the termin-
  37. ation of the move, the move is checked against the possible moves.  If it is
  38. a valid move, the board is updated and displayed.
  39.  
  40. Return Values
  41. -------------
  42.    HASNT_MOVED: complete move not entered, yet.
  43.    valid move:  complete move entered.
  44. -----------------------------------------------------------------------------*/
  45.  
  46. move_type check_for_input ()
  47. {
  48. register int key;        /* key pressed */
  49. register int stat;        /* status from key */
  50.  
  51. #define ST_STAT_OK    0    /* ok status -- do nothing */
  52. #define ST_ROW_ERR    1    /* row already entered error */
  53. #define ST_COL_ERR    2    /* col already entered error */
  54. #define ST_BAD_KEY    3    /* bad key error */
  55.  
  56. /*-----------------------------------------------
  57. First off, check to see if the human can make a
  58. move.  If he can't, return the NO_MOVE_MASK.
  59. -----------------------------------------------*/
  60.  
  61. if (input_flag)
  62.   {
  63.   input_flag = 0;
  64.  
  65.   if (!(who_can_move (&curnt_bd) & THEM_PIECE))
  66.     {
  67.     disp_player_cant_move (THEM_PIECE);
  68.     disp_board (&curnt_bd);
  69.     disp_player_move (US_PIECE);
  70.     return (NO_MOVE_MASK);
  71.     }
  72.  
  73.   disp_player_move (THEM_PIECE);
  74.   }
  75.  
  76.  
  77. /*-----------------------------------------------
  78. -----------------------------------------------*/
  79.  
  80.  
  81. if (kbhit () == 0)        /* if no key has been pressed, return */
  82.   return (HASNT_MOVED);        /* move not completely entered */
  83.  
  84. key = getch ();            /* get key pressed */
  85. key = toupper (key);        /* convert key to uppercase */
  86.  
  87. stat = ST_STAT_OK;        /* init status to OK */
  88.  
  89.  
  90. /*-----------------------------------------------
  91. -----------------------------------------------*/
  92.  
  93. if (key >= '1' && key <= '8')        /* is key a column number? */
  94.   {
  95.   if (move_col == -1)
  96.     disp_col (move_col = key - '1');
  97.   else
  98.     stat = ST_COL_ERR;
  99.   }
  100.  
  101. else if (key >= 'A' && key <= 'H')    /* is key a row number? */
  102.   {
  103.   if (move_row == -1)
  104.     disp_row (move_row = key - 'A');
  105.   else
  106.     stat = ST_ROW_ERR;
  107.   }
  108.  
  109. else switch (key)            /* check other keys */
  110.   {
  111.   case '\b':                /* back-space ? */
  112.     move_col = move_row = -1;
  113.     disp_clr_row_col ();
  114.     break;
  115.  
  116.   default:                /* unrecognized key */
  117.     stat = ST_BAD_KEY;
  118.   }
  119.  
  120.  
  121. /*-----------------------------------------------
  122. -----------------------------------------------*/
  123.  
  124. switch (stat)
  125.   {
  126.   case ST_COL_ERR:
  127.     disp_error_msg ("Column Already Entered");
  128.     goto ERROR_WRAP_UP;
  129.  
  130.   case ST_BAD_KEY:
  131.     disp_error_msg ("Invalid Key Entered");
  132.     goto ERROR_WRAP_UP;
  133.  
  134.   case ST_ROW_ERR:
  135.     disp_error_msg ("Row Already Entered");
  136.  
  137.   ERROR_WRAP_UP:
  138.     disp_board (&curnt_bd);
  139.     disp_player_move (THEM_PIECE);
  140.     if (move_row != -1)
  141.       disp_row (move_row);
  142.     if (move_col != -1)
  143.       disp_col (move_col);
  144.  
  145.   default:
  146.     break;
  147.   }
  148.  
  149.  
  150. /*-----------------------------------------------
  151. -----------------------------------------------*/
  152.  
  153. if (move_row != -1 && move_col != -1)
  154.   {
  155.   move_type curnt_move;
  156.  
  157.   curnt_move = 0;
  158.  
  159.   SET_ROW_COL (curnt_move, move_row, move_col);
  160.  
  161.   if (verify_move_and_update_board (curnt_move, THEM_PIECE))
  162.     {
  163.     move_row = move_col = -1;
  164.     disp_error_msg ("Invalid Move.  You Can't Move There.");
  165.     goto ERROR_WRAP_UP;
  166.     }
  167.  
  168.   disp_player_move (US_PIECE);
  169.   move_row = move_col = -1;
  170.   return (curnt_move);            /* move completed and updated */
  171.   }
  172.  
  173. return (HASNT_MOVED);            /* move not completely entered */
  174. }
  175.  
  176.  
  177. /*-----------------------------------------------------------------------------
  178. -----------------------------------------------------------------------------*/
  179.  
  180. void init_input_vars ()
  181. {
  182. move_row = -1;
  183. move_col = -1;
  184. input_flag = -1;
  185. }
  186.  
  187.  
  188. /*-----------------------------------------------------------------------------
  189. -----------------------------------------------------------------------------*/
  190.  
  191.